<?php

namespace IDock\Insurance\AMIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\AMIC\AMICEnums;
use IDock\Insurance\AMIC\Response\AllAMICResponse;
use IDock\Printer\EPolicyDownloadPrinter;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllAMICResponse
{

    /**
     * @var RenewalInquiryPrinter $printer
     */
    public $printer = RenewalInquiryPrinter::class;

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();
        $body = $this->getBody()['Body'] ?? '';
        $renewBase = $body['renewBase'] ?? [];
        $renewalInquiryPrinter->argc['orderNo'] = $renewBase['orderNo'] ?? '';
        $oldBusiFlag = $body['renewBase']['oldBusiFlag'] ?? '';
        if ($oldBusiFlag) {
            $renewalInquiryPrinter->setRenewalFlg(true);
        } else {
            $this->printer = $renewalInquiryPrinter;
            return $this;
        }
        $biData = $body['oldBusiPolicy'] ?? [];
        $ciData = $body['oldCIPolicy'] ?? [];
        $this->printer->biPolicyNo = $biData['oldBusiPolicyNO'] ?? '';
        $this->printer->biEndData = $biData['oldBusiEndDate'] ? date('Y-m-d',strtotime($biData['oldBusiEndDate'])) : '';
        $this->printer->ciPolicyNo = $ciData['oldCIPolicyNO'] ?? '';
        $this->printer->ciEndData = $ciData['oldCIEndDate'] ? date('Y-m-d',strtotime($ciData['oldCIEndDate'])) : '';

        $vehicle = $body['vehicle'] ?? [];
        //车牌号
        $renewalInquiryPrinter->licenseNo = $vehicle['licensePlateNo'] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['vin'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['engineNo'] ?? '';
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['firstRegisterDate'] ?? '';
        //车辆信息
        $vehiclePrinter = new VehiclePrinter();
        $vehiclePrinter->vehicleName = $vehicle['vehicleName'] ?? '';
        $vehiclePrinter->power = $vehicle['Power'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['vehicleActualPrice'] ?? '';
        $vehiclePrinter->vehicleExhaust = $vehicle['displaceMent'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['seatCount'] ?? '';
        $vehiclePrinter->vehicleTonnage = round(floatval($vehicle['limitLoad'] ?? '') * 1000,2);
        $vehiclePrinter->wholeWeight = $vehicle['wholeWeight'] ?? '';
        $vehiclePrinter->vehicleModelCode = $vehicle['vehicleId'] ?? '';
        $vehiclePrinter->energyTypeCode =  AMICEnums::$renewFuelType[$vehicle['fuelType'] ?? 'A'];
        if (in_array($vehiclePrinter->energyTypeCode,['E02','E03','E04'])) {
            $vehiclePrinter->isNewEnergy = '1';
        }


        //投保人
        $applicantPrinter = new PremiumRelationPrinter();
        $applicant = $body['applicant'] ?? [];
        $applicantPrinter->name = $applicant['appName'] ?? '';
        $applicantPrinter->mobile = $applicant['appMobile'] ?? '';
        $applicantPrinter->address = $applicant['appAddress'] ?? '';
        $applicantPrinter->certificateNo = $applicant['certfCode'] ?? '';
        $applicantPrinter->certificateTypeCode = array_keys(AMICEnums::$certificateTypeCode, $applicant['certfCls'] ?? '')[0] ?? '';
        $applicantPrinter->email = $applicant['appEmail'] ?? '';
        $applicantPrinter->certificateStartDate = $applicant['certfValidStartDate'] ?? '';
        $applicantPrinter->certificateEndDate = $applicant['certfValidEndDate'] ?? '';
        $applicantPrinter->typeCode = $applicantPrinter->certificateTypeCode == '01' ? '01' : '03';

        //被保人
        $insuredPrinter = new PremiumRelationPrinter();
        $insured = $body['appInsured'] ?? [];
        $insuredPrinter->name = $insured['insuredName'] ?? '';
        $insuredPrinter->mobile = $insured['insuredMobile'] ?? '';
        $insuredPrinter->address = $insured['insuredAddress'] ?? '';
        $insuredPrinter->certificateNo = $insured['certfCode'] ?? '';
        $insuredPrinter->certificateTypeCode = array_keys(AMICEnums::$certificateTypeCode, $insured['certfCls'] ?? '')[0] ?? '';
        $insuredPrinter->email = $insured['insuredEmail'] ?? '';
        $insuredPrinter->certificateStartDate = $insured['certfValidStartDate'] ?? '';
        $insuredPrinter->certificateEndDate = $insured['certfValidEndDate'] ?? '';
        $insuredPrinter->typeCode = $insuredPrinter->certificateTypeCode == '01' ? '01' : '03';

        //车主
        $ownerPrinter = new PremiumRelationPrinter();
        $owner = $body['vehicleOwner'] ?? [];
        $ownerPrinter->name = $owner['vehicleOwnerName'] ?? '';
        $ownerPrinter->mobile = $owner['vehicleOwnerMobile'] ?? '';
        $ownerPrinter->address = $owner['vehicleOwnerAddress'] ?? '';
        $ownerPrinter->certificateNo = $owner['vehicleOwnerIdentifyNumber'] ?? '';
        $ownerPrinter->certificateTypeCode = array_keys(AMICEnums::$certificateTypeCode, $owner['vehicleOwnerIdentifyType'] ?? '')[0] ?? '';
        $ownerPrinter->email = $owner['vehicleOwnerEmail'] ?? '';
        $ownerPrinter->certificateStartDate = $owner['certfValidStartDate'] ?? '';
        $ownerPrinter->certificateEndDate = $owner['certfValidEndDate'] ?? '';
        $ownerPrinter->typeCode = $ownerPrinter->certificateTypeCode == '01' ? '01' : '03';
        $coverageList = $body['renewItemKind']['coverageItem'] ?? [];
        if(!isset($coverageList[0])){
            $tmp = $coverageList;
            unset($coverageList);
            $coverageList[] = $tmp;
        }
        foreach ($coverageList as $coverage) {
            $coveragePrinter = new PremiumCoveragePrinter();
            $coveragePrinter->coverageCode = array_search($coverage['coverageCode'] ?? '', AMICEnums::$coverageCode);
            if (!$coveragePrinter->coverageCode) {
                continue;
            }
            if(in_array($vehiclePrinter->energyTypeCode,['E02','E03','E04'])){
                $coveragePrinter->coverageCode = 'C1' . substr($coveragePrinter->coverageCode, 2, 4);
            }
            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = (string)(int)($coverage['sumLimit'] ?? '');
            $coveragePrinter->unitAmount = (string)(int)($coverage['unitAmount'] ?? '');
            $coveragePrinter->serviceTimes = $coverage['serverTime'] ?? '';
            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }
        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }
}
